




#include"gl_GUI.h"
#include"gl_TextFont.h"


/// <summary>
/// 双向链表结构体。
/// </summary>
typedef struct  __GUI_dList__
{
	/// <summary>
	/// 链表节点数量。
	/// </summary>
	int count; 
		
	/// <summary>
	/// 如果是按序插入，就是从小往大排列，头节点是最小的节点。
	/// </summary>
	GUI_dlistNode* head;  
	/// <summary>
	/// 如果是按序插入，就是从小往大排列，尾节点是最大的节点。
	/// </summary>
	GUI_dlistNode* tail;   
}GUI_dlist;

/// <summary>
/// offset_of(struct_type, mem_name)：计算结构体struct_type里的元素mem_name相对结构体的偏移地址。
/// 原理：0被强制转换为struct_type的指针类型，这个struct_type的地址是0，mem_name的地址就是相对0的偏移地址，也是相对结构体的偏移地址。
/// </summary>
#define GUI_offset_of(struct_type, mem_name)  (int)&(((struct_type*)0)->mem_name)

/// <summary>
/// container_of：计算包含节点指针ptr的结构体。结构体为type，节点在结构体内的名称是member。
/// </summary>
#define GUI_container_of(ptr, type, member) (type *)((char *)(ptr) - GUI_offset_of(type,member) )

/// <summary>
/// 初始化链表
/// </summary>
/// <param name="list">链表结构体</param>
/// <param name="c">链表节点的比较函数。在有序插入的时候要用到。</param>
void GUI_dlist_init(GUI_dlist* list)//, int (*c)(dlistNode*, dlistNode*))
{
	list->count = 0;
	list->head = NULL;
	list->tail = NULL;
}

/// <summary>
/// 在双向链表尾部插入一个元素。
/// </summary>
/// <param name="list">链表结构体</param>
/// <param name="new_element">新插入的节点</param>
/// <returns>是否操作成功</returns>
bool GUI_dlist_push_back(GUI_dlist* list, GUI_dlistNode* new_element)
{
	if (list->count == 0) {
		// 处理空双向链表
		list->head = new_element;
		list->tail = new_element;
		list->head->prev = NULL;
		list->head->next = NULL;
	}
	else {
		// 处理非空双向链表
		new_element->next = NULL;
		new_element->prev = list->tail;
		list->tail->next = new_element;
		list->tail = new_element;
	}
	// 更新链表的 数量
	list->count++;
	return true;
}
//

//  GUI 需要用到的链表操作都在上边。直接从c_dlist复制过来。加上GUI_前缀，避免引用出问题。

/// <summary>
/// 按键列表
/// </summary>
GUI_dlist list_Button;
/// <summary>
/// 滑块链表
/// </summary>
GUI_dlist list_ScrollBar;   


//Style default_hovor_style = {  // 触摸到控件的样式
//	.line_width = 2
//	.line_color = RGB_ctrl_purple,
//	.back_color = RGB_crtl_blue,
//};
//Style default_leave_style = {  // 离开控件的样式
//	.line_width = 2
//	.line_color = RGB_line_blue,
//	.back_color = RGB_crtl_blue,
//};
//Style default_window_style = {  // 窗口样式
//	.line_width = 2
//	.line_color = RGB_line_blue,
//	.back_color = RGB_window_blue,
//};

Style default_hovor_style = {  // 触摸到控件的样式
	 3,
	 RGB_ctrl_purple,
	 RGB_crtl_blue,
};
Style default_leave_style = {  // 离开控件的样式
	2,
	RGB_line_blue,
	RGB_crtl_blue,
};
Style default_window_style = {  // 窗口样式
	 2,
	 RGB_line_blue,
	 RGB_window_blue,
};

/// <summary>
/// 初始化GUI_Location。默认坐标（0，0）
/// </summary>
/// <param name="location">位置结构体变量</param>
void location_init(GUI_Location* location)
{
	location->left = 0;
	location->top = 0;
}

/// <summary>
/// 初始化GUI_Size。默认大小：（0，0）
/// </summary>
/// <param name="size">尺寸结构体变量</param>
void size_init(GUI_Size* size)
{
	size->height = 0;
	size->width = 0;
}

/// <summary>
/// 使用位置和大小初始化区域。
/// </summary>
/// <param name="area">待初始化的区域结构体变量</param>
/// <param name="location">位置结构体</param>
/// <param name="size">尺寸的结构体</param>
void area_init(GUI_Area* area, GUI_Location* location, GUI_Size* size)
{
	area->left = location->left;
	area->top = location->top;
	area->right = area->left + size->width;
	area->bottom = area->top + size->height;
}
/// <summary>
/// 判断（x,y）坐标是否在区域area内。
/// </summary>
/// <param name="area">区域</param>
/// <param name="x">坐标x轴</param>
/// <param name="y">坐标y轴</param>
/// <returns>如果在区域内返回true，否则返回false。</returns>
bool is_enter_area(GUI_Area* area, int x, int y)
{
	if (x >= area->left && x<=area->right && y>=area->top && y <= area->bottom)return true;
	else
	{
		return false;
	}
}



/// <summary>
/// 初始化标签文本
/// </summary>
/// <param name="lable">标签指针</param>
/// <param name="text">标签文本</param>
/// <param name="text_len">标签文本长度</param>


/// <summary>
/// 初始化标签文本
/// </summary>
/// <param name="lable">标签指针</param>
/// <param name="left">标签位置x坐标</param>
/// <param name="top">标签位置y坐标</param>
/// <param name="w">标签宽度</param>
/// <param name="h">标签高度</param>
void lable_init(Lable* lable,int left,int top,int w,int h)
{
	lable->area.left = left;
	lable->area.top = top;
	lable->area.right = left + w;
	lable->area.bottom = top + h;

	lable->is_show_edge = true;
	lable->style = default_leave_style;

	lable->background_icon.buffer = NULL;
	lable->background_icon.width = 0;
	lable->background_icon.height = 0;
	lable->background_location.left = 0;
	lable->background_location.top = 0;
}
/// <summary>
/// 初始化标签文本
/// </summary>
/// <param name="lable">标签指针</param>
/// <param name="text">标签文本</param>
/// <param name="text_len">标签文本长度</param>
/// <param name="textfont">文本样式</param>
void lable_init_text(Lable* lable, const char* text, int text_len,TextFont textfont)
{
	int i = 0;
	if (text_len > Lable_Max_Text_Length)text_len = Lable_Max_Text_Length;
	lable->text_Length = text_len;
	for (i = 0; i < text_len; i++) {
		lable->text[i] = text[i];
	}
	lable->textfont = textfont;
}
/// <summary>
/// 绘制标签
/// </summary>
/// <param name="lable">待绘制的标签</param>
void lable_draw(Lable* lable)
{	
	int w = lable->area.right - lable->area.left;
	int h = lable->area.bottom - lable->area.top;
	if (lable->is_show_edge) {
		draw_rectangle(lable->area.left, lable->area.top,
			w, h,
			lable->style.line_color);
	}

	if (lable->background_icon.buffer != NULL) {
		draw_string_in_area_no_frame_on_background(lable->area.left + 1, lable->area.top + 1, w - 1, h - 1,
			lable->text, lable->text_Length, lable->textfont,
			lable->background_icon, lable->background_location.left, lable->background_location.top);
	}
	else
	{
		draw_string_in_area_no_frame(lable->area.left + 1, lable->area.top + 1, w - 1, h - 1,
			lable->text, lable->text_Length, lable->textfont);
	}

	// draw_string(lable->location.left, lable->location.top, lable->text, lable->text_Length, lable->textfont);
}

/// <summary>
/// 绘制标签边框
/// </summary>
/// <param name="lable">待绘制的标签</param>
void lable_draw_edge(Lable* lable)
{
	int w = lable->area.right - lable->area.left;
	int h = lable->area.bottom - lable->area.top;
	// if (lable->is_show_edge) 
	{
		draw_rectangle(lable->area.left, lable->area.top,
			w, h,
			lable->style.line_color);
	}

}


/// <summary>
/// 绘制离开按键的样式
/// </summary>
/// <param name="button">按键</param>
void button_draw_leave(Button* button)
{
	COLOR_RGB line_color = button->leave.line_color;
	COLOR_RGB back_color = button->leave.back_color;
	int line_width = button->leave.line_width;

	int width = button->area.right - button->area.left;
	int height = button->area.bottom - button->area.top;
	//绘制离开的样式
	//先判断背景色与hovor的样式是否一致。如果一致，重绘边框即可，提高绘制速度。
	if (button->leave.back_color == button->hovor.back_color && button->leave.line_width == button->hovor.line_width) {
		draw_roundedrectangle(button->area.left, button->area.top, width, height, button->r, line_width, line_color);
	//	draw_roundedrectangle(button->area.left, button->area.top, width, height, button->r, line_color);
	}
	else {
		draw_solidroundedrectangle(button->area.left, button->area.top, width, height, button->r, back_color);
		draw_roundedrectangle(button->area.left, button->area.top, width, height, button->r, line_width, line_color);

	}

}
/// <summary>
/// 绘制进入按键的样式
/// </summary>
/// <param name="button">按键</param>
void button_draw_hovor(Button* button)
{
	COLOR_RGB line_color = button->hovor.line_color;
	COLOR_RGB back_color = button->hovor.back_color;
	int line_width = button->hovor.line_width;
	int width = button->area.right - button->area.left;
	int height = button->area.bottom - button->area.top;

	//先判断背景色与leave的样式是否一致。如果一致，重绘边框即可，提高绘制速度。
	if (button->leave.back_color == button->hovor.back_color && button->leave.line_width == button->hovor.line_width) {
		draw_roundedrectangle(button->area.left, button->area.top, width, height, button->r, line_width, line_color);
	//	draw_roundedrectangle(button->area.left, button->area.top, width, height, button->r, line_color);

	}
	else {
		draw_solidroundedrectangle(button->area.left, button->area.top, width, height, button->r, back_color);
		draw_roundedrectangle(button->area.left, button->area.top, width, height, button->r, line_width, line_color);

	}
}

/// <summary>
/// 按键的消息响应函数
/// </summary>
/// <param name="button">按键</param>
/// <param name="msg">消息</param>
void button_onMouseMsg(Button* button)
{
	// 调用用户指定的回调函数
	if (button->onClick)  //如果没有定义回调函数，就不执行。
	{
		button->onClick();
	}
}
/// <summary>
/// 按键初始化函数。样式使用默认样式。初始化后将控件插入链表。
/// </summary>
/// <param name="button">按键</param>
/// <param name="x">按键位置x坐标</param>
/// <param name="y">按键位置y坐标</param>
/// <param name="w">按键长</param>
/// <param name="h">按键宽</param>
void button_init(Button* button, int x, int y, int w, int h)
{
	button->location.left = x;
	button->location.top = y;
	button->size.width = w;
	button->size.height = h;

	button->r = (w > h) ? h / 3 : w / 3;

	area_init(&button->area, &button->location, &button->size);
	button->lable = NULL;
	button->is_hovor = false;
	button->onClick = NULL;
	GUI_dlist_push_back(&list_Button, &button->node);
	
	button->leave = default_leave_style;
	button->hovor = default_hovor_style;
}
/// <summary>
/// 按键第一次绘制。
/// </summary>
/// <param name="button">按键</param>
void button_firstdraw(Button* button)
{
	if (button->lable != NULL)lable_draw(button->lable);
	//第一次绘制按键。
	COLOR_RGB line_color = button->leave.line_color;
	COLOR_RGB back_color = button->leave.back_color;
	int line_width = button->leave.line_width;
	int width = button->area.right - button->area.left;
	int height = button->area.bottom - button->area.top;

	draw_solidroundedrectangle(button->area.left, button->area.top, width, height, button->r, back_color);
	draw_roundedrectangle(button->area.left, button->area.top, width, height, button->r, line_width, line_color);

//	draw_roundedrectangle(button->area.left, button->area.top, width, height, button->r, line_color);

}



/// <summary>
/// 绘制实心圆头的竖条
/// </summary>
/// <param name="r1">圆头的大半径</param>
/// <param name="r2">圆头的小半径，圆头的边框宽度=r1-r2</param>
/// <param name="cx">圆心x坐标</param>
/// <param name="c_t">顶部圆心y坐标</param>
/// <param name="c_b">底部圆心y坐标</param>
/// <param name="line_color">边框颜色</param>
/// <param name="fill_color">填充色</param>
void fill_bar_vertical(int r1, int r2, int c_x, int c_t, int c_b, COLOR_RGB line_color, COLOR_RGB fill_color)
{
	if (r1 < r2) { int t = r2; r2 = r1; r1 = t; }
	int l = c_x - r1;
	int t = c_t - r1;
	int w = r1 * 2 + 1;
	int h = w + c_b - c_t;

	draw_roundedrectangle(l, t, w, h, r1, r1 - r2, line_color);

	if (r2 < r1) {
		l = c_x - r2;
		t = c_t - r2;
		w = r2 * 2 + 1;
		h = w + c_b - c_t;
		draw_roundedrectangle(l, t, w, h, r2, w, fill_color);
	}



}

/// <summary>
/// 绘制实心圆头的横条
/// </summary>
/// <param name="r1">圆头的大半径</param>
/// <param name="r2">圆头的小半径，圆头的边框宽度=r1-r2</param>
/// <param name="c_y">圆心y坐标</param>
/// <param name="c_l">左侧圆心x坐标</param>
/// <param name="c_r">右侧圆心x坐标</param>
/// <param name="line_color">边框颜色</param>
/// <param name="fill_color">填充色</param>
void fill_bar_horizontal(int r1, int r2, int c_y, int c_l, int c_r, COLOR_RGB line_color, COLOR_RGB fill_color)
{
	if (r1 < r2) { int t = r2; r2 = r1; r1 = t; }
	int l = c_l - r1;
	int t = c_y - r1;
	int h = r1 * 2 + 1; 
	int w = c_r - c_l + h;

	draw_roundedrectangle(l, t, w, h, r1, r1 - r2, line_color);

	if (r2 < r1) {
		 l = c_l - r2;
		 t = c_y - r2;
		 h = r2 * 2 + 1;
		 w = c_r - c_l + h;
		 draw_roundedrectangle(l, t, w, h, r2, w, fill_color);
	}

}
//

/// <summary>
/// 绘制空心圆头的竖条
/// </summary>
/// <param name="r1">圆头的大半径</param>
/// <param name="r2">圆头的小半径，圆头的边框宽度=r1-r2</param>
/// <param name="cx">圆心x坐标</param>
/// <param name="c_t">顶部圆心y坐标</param>
/// <param name="c_b">底部圆心y坐标</param>
/// <param name="line_color">边框颜色</param>
void draw_bar_vertical(int r1, int r2, int c_x, int c_t, int c_b, COLOR_RGB line_color)
{
	if (r1 < r2) { int t = r2; r2 = r1; r1 = t; }
	int l = c_x - r1;
	int t = c_t - r1;
	int w = r1 * 2 + 1;
	int h = w + c_b - c_t;

	draw_roundedrectangle(l, t, w, h, r1, r1 - r2, line_color);

}

/// <summary>
/// 绘制空心圆头的横条
/// </summary>
/// <param name="r1">圆头的大半径</param>
/// <param name="r2">圆头的小半径，圆头的边框宽度=r1-r2</param>
/// <param name="cy">圆心y坐标</param>
/// <param name="c_l">左侧圆心x坐标</param>
/// <param name="c_r">右侧圆心x坐标</param>
/// <param name="line_color">边框颜色</param>
void draw_bar_horizontal(int r1, int r2, int c_y, int c_l, int c_r, COLOR_RGB line_color)
{

	if (r1 < r2) { int t = r2; r2 = r1; r1 = t; }

	int l = c_l - r1;
	int t = c_y - r1;
	int h = r1 * 2 + 1;
	int w = c_r - c_l + h;

	draw_roundedrectangle(l, t, w, h, r1, r1 - r2, line_color);

}
//


/// <summary>
/// 离开滚动条的样式
/// </summary>
/// <param name="scrollbar">滚动条</param>
void scrollbar_draw_leave(ScrollBar* scrollbar)
{
	if (scrollbar->status) {  // 竖直状态。
		int line_w = scrollbar->leave.line_width;

		int left = scrollbar->area.left + (scrollbar->size.width  - scrollbar->thickness) / 2 - line_w;
		int top = scrollbar->area.top;
		int w = scrollbar->thickness + line_w * 2;
//		int h = scrollbar->size.height;
		int r = w / 2;
		//draw_rounded_rectangle_boundary(left, top, w, h, r, scrollbar->leave.line_width, scrollbar->leave.line_color);

		int r2 = w / 2;                               // 大半径
		int r1 = r2 - scrollbar->leave.line_width;  // 小半径
		int c_l = left + w / 2;                // 圆心的上下左右位置
		int c_t = top + r;
		int c_b = top + r + scrollbar->max_value;
		draw_bar_vertical(r1, r2, c_l, c_t, c_b, scrollbar->leave.line_color);
	}
	else {   // 横状态
		int line_w = scrollbar->leave.line_width;

		int left = scrollbar->area.left;
		int top = scrollbar->area.top + (scrollbar->size.height - scrollbar->thickness )/ 2 - line_w;
		int h = scrollbar->thickness + line_w * 2;
//		int w = scrollbar->size.width;
		int r = h / 2;  //边框圆角半径
		//draw_rounded_rectangle_boundary(left, top, w, h, r, scrollbar->leave.line_width, scrollbar->leave.line_color);

		int r2 = h / 2;                               // 大半径
		int r1 = r2 - scrollbar->leave.line_width;  // 小半径
		int c_l = left + r;                // 圆心的上下左右位置
		int c_r = left + r + scrollbar->max_value;
		int c_t = top + h / 2;
		draw_bar_horizontal(r1, r2, c_t, c_l, c_r, scrollbar->leave.line_color);

	}
}
/// <summary>
/// 进入滚动条的样式
/// </summary>
/// <param name="scrollbar">滚动条</param>
void scrollbar_draw_hovor(ScrollBar* scrollbar)
{
	if (scrollbar->status) {  // 竖直状态。
		int line_w = scrollbar->hovor.line_width;

		int left = scrollbar->area.left + (scrollbar->size.width  - scrollbar->thickness )/ 2 - line_w;
		int top = scrollbar->area.top;
		int w = scrollbar->thickness + line_w * 2;
//		int h = scrollbar->size.height;
		int r = w / 2;
		//draw_rounded_rectangle_boundary(left, top, w, h, r, scrollbar->hovor.line_width, scrollbar->hovor.line_color);

		int r2 = w / 2;                               // 大半径
		int r1 = r2 - scrollbar->hovor.line_width;  // 小半径
		int c_l = left + w / 2;                // 圆心的上下左右位置
		int c_t = top + r;
		int c_b = top + r + scrollbar->max_value;
		draw_bar_vertical(r1, r2, c_l, c_t, c_b, scrollbar->hovor.line_color);
	
	}
	else {   // 横状态
		int line_w = scrollbar->hovor.line_width;

		int left = scrollbar->area.left;
		int top = scrollbar->area.top + (scrollbar->size.height - scrollbar->thickness) / 2 - line_w;
		int h = scrollbar->thickness + line_w * 2;
//		int w = scrollbar->size.width;
		int r = h / 2;  //边框圆角半径
		//draw_rounded_rectangle_boundary(left, top, w, h, r, scrollbar->hovor.line_width, scrollbar->hovor.line_color);

		int r2 = h / 2;                               // 大半径
		int r1 = r2 - scrollbar->hovor.line_width;  // 小半径
		int c_l = left + r;                // 圆心的上下左右位置
		int c_r = left + r + scrollbar->max_value;
		int c_t = top + h / 2;
		draw_bar_horizontal(r1, r2, c_t, c_l, c_r, scrollbar->hovor.line_color);

	}
}

/// <summary>
/// 绘制滚动条移动的样式。按下的时候才能移动滑块，所以用按下的样式。
/// </summary>
/// <param name="scrollbar">滚动条</param>
/// <param name="value0">滚动条初始值，需要控制参数是合法的值</param>
/// <param name="value1">滚动条滚动后的值，需要控制参数是合法的值</param>
void scrollbar_draw_move(ScrollBar* scrollbar, int value0, int value1)   // 这里只滑动一个圆
{
	if (value0 == value1)return;

	if (scrollbar->status) { //竖滑块
		int line_w = scrollbar->leave.line_width;

		int left = scrollbar->area.left + (scrollbar->size.width - scrollbar->thickness) / 2 - line_w;
		int top = scrollbar->area.top;
		int w = scrollbar->thickness + line_w * 2;
//		int h = scrollbar->size.height;
		int r =  scrollbar->thickness / 2;  // 滑块半径
		int rr = r * r;
		int cx = left + w / 2;
		int cy1 = top + line_w + r;
		int cy2 = top + line_w + r + scrollbar->max_value;

		Bresenham_circle_iterator_init(x, y, r);
		for (int i = x; i < r; i++) 
		{		
			// int s = sqrtf(rr - i * i);
			while (i != x) { Bresenham_circle_iterator(x, y); } int s = y;

			if (value0 > value1) {			
				draw_line_vertical(cx + i, cy1 + value1 + s, cy1 + value0 + s, 1, scrollbar->background_color); //消隐滚动前的滑块
				draw_line_vertical(cx - i, cy1 + value1 + s, cy1 + value0 + s, 1, scrollbar->background_color);
				
			}
			else {
				draw_line_vertical(cx + i, cy1 + value1 - s, cy1 + value0 - s, 1, scrollbar->background_color); //消隐滚动前的滑块
				draw_line_vertical(cx - i, cy1 + value1 - s, cy1 + value0 - s, 1, scrollbar->background_color);
			}

			draw_line_vertical(cx + i, cy1 + value1 - s, cy1 + value1 + s, 1, scrollbar->slider_color); // 绘制滚动后的滑块
			draw_line_vertical(cx - i, cy1 + value1 - s, cy1 + value1 + s, 1, scrollbar->slider_color);
	
		}
	}
	else { //横滑块
		int line_w = scrollbar->hovor.line_width;

		int left = scrollbar->area.left;
		int top = scrollbar->area.top + (scrollbar->size.height - scrollbar->thickness )/ 2 - line_w;
		int h = scrollbar->thickness + line_w * 2;
//		int w = scrollbar->size.width;
		int r = scrollbar->thickness / 2;  // 滑块半径
		int rr = r * r;
		int cy = top + h / 2;
		int cx1 = left + line_w + r;
		int cx2 = left + line_w + r + scrollbar->max_value;
		Bresenham_circle_iterator_init(x, y, r);
		for (int i = 0; i < r; i++) {
			// int s = sqrtf(rr - i * i);
			while (i != x) { Bresenham_circle_iterator(x, y); } int s = y;

			if (value0 > value1) {
				draw_line_horizontal(cy + i, cx1 + value1 + s, cx1 + value0 + s, 1, scrollbar->background_color); //消隐滚动前的滑块
				draw_line_horizontal(cy - i, cx1 + value1 + s, cx1 + value0 + s, 1, scrollbar->background_color);
			}
			else {
				draw_line_horizontal(cy + i, cx1 + value1 - s, cx1 + value0 - s, 1, scrollbar->background_color); //消隐滚动前的滑块
				draw_line_horizontal(cy - i, cx1 + value1 - s, cx1 + value0 - s, 1, scrollbar->background_color);
			}

			draw_line_horizontal(cy + i, cx1 + value1 - s, cx1 + value1 + s, 1, scrollbar->slider_color); // 绘制滚动后的滑块
			draw_line_horizontal(cy - i, cx1 + value1 - s, cx1 + value1 + s, 1, scrollbar->slider_color);
		}
	}
}

/// <summary>
/// 滚动条消息响应函数。此时
/// </summary>
/// <param name="scrollbar">滚动条</param>
/// <param name="x">当前触摸的x坐标</param>
/// <param name="y">当前触摸的y坐标</param>
void scrollbar_onMouseMsg(ScrollBar* scrollbar,int x,int y)
{
	//更新MousePoint
	scrollbar->lastMousePoint = scrollbar->newMousePoint;
	if (scrollbar->status == 0) //横，记录x坐标
	{
		scrollbar->newMousePoint = x;
	}
	else
	{
		scrollbar->newMousePoint = y;
	}
	{
		//更新滑块值
		int value0 = scrollbar->value;
		int value1 = value0 + (scrollbar->newMousePoint - scrollbar->lastMousePoint);
		if (value1 > scrollbar->max_value)value1 = scrollbar->max_value;
		if (value1 < 0)value1 = 0;
		scrollbar_draw_move(scrollbar, value0, value1);
		scrollbar->value = value1;
	}

	// 调用用户指定的回调函数
	if ((scrollbar->onClick))  //如果没有定义回调函数，就不执行。
	{
		scrollbar->onClick();
	}
}
/// <summary>
/// 滚动条控件初始化。样式使用默认样式。初始化后将控件插入链表。
/// </summary>
/// <param name="scrollbar">滚动条控件</param>
/// <param name="x">滚动条位置x轴坐标</param>
/// <param name="y">滚动条位置y轴坐标</param>
/// <param name="w">滚动条位置宽</param>
/// <param name="h">滚动条位置高</param>
void scrollbar_init(ScrollBar* scrollbar, int x, int y, int w, int h)
{
	// 
	if (w < 10)w = 10;  // 这里限制滑块的最小尺寸。
	if (h < 10)h = 10;  // 再小就没有使用意义了。

	scrollbar->status = (w > h) ? 0 : 1;		//如果不设置，则根据滑块尺寸自动判断状态。0表示横，1表示竖。设置status会自动调整size，使得滑块至少有空间。
	scrollbar->leave = default_leave_style;
	scrollbar->hovor = default_hovor_style;

	scrollbar->location.left = x;
	scrollbar->location.top = y;
	scrollbar->size.width = w;
	scrollbar->size.height = h;

	scrollbar->background_color = RGB_white;
	scrollbar->slider_color = RGB_green;
	scrollbar->is_reverse = false;

	//
	int line_width = 2;
	scrollbar->hovor.line_width = line_width;  //滑块固定线宽。
	scrollbar->leave.line_width = line_width;  //滑块固定线宽。
	scrollbar->thickness = (w > h) ? (h - 4) / 2 : (w - 4) / 2 ;	// 滑块的厚度。是固定值Thickness_Of_Bar。  |_____■_____________|
	scrollbar->newMousePoint = 0;
	scrollbar->lastMousePoint = 0;				//记录鼠标的位置。如果status是0记录x坐标；如果status是1记录y坐标。
	scrollbar->value = 0;						//滑块的值，最小是0.
	scrollbar->max_value = (scrollbar->status == 0) ? (w - scrollbar->thickness - 4) : (h - scrollbar->thickness - 4);  //最大的值，根据滑块大小确定。

	area_init(&scrollbar->area, &scrollbar->location, &scrollbar->size);

	scrollbar->lable = NULL;
	scrollbar->is_hovor = false;
	scrollbar->onClick = NULL;
	GUI_dlist_push_back(&list_ScrollBar, &scrollbar->node);
}
/// <summary>
/// 滚动条第一次绘制
/// </summary>
/// <param name="scrollbar">滚动条</param>
void scrollbar_firstdraw(ScrollBar* scrollbar)
{	
	//先绘制边框。后面 leave，hovor 的状态，直接更改线框即可。

	// 画在中间。

	if (scrollbar->status) {  // 竖直状态。
		int line_w = scrollbar->leave.line_width;

		int left = scrollbar->area.left + (scrollbar->size.width  - scrollbar->thickness) / 2 - line_w;
		int top = scrollbar->area.top;
		int w = scrollbar->thickness + line_w * 2;
//		int h = scrollbar->size.height;
		int r = w / 2;

		int r2 = w / 2;                               // 大半径
		int r1 = r2 - scrollbar->leave.line_width;  // 小半径
		int c_l = left + w / 2;                // 圆心的上下左右位置
		int c_t = top + r;
		int c_b = top + r + scrollbar->max_value;
		fill_bar_vertical(r1, r2, c_l, c_t, c_b, scrollbar->leave.line_color, scrollbar->background_color);


		// 绘制中间的滑块。
		r = scrollbar->thickness / 2;  // 滑块半径
		int rr = r * r;
		int cx = left + w / 2;
		int cy1 = top + line_w + r;
		int cy2 = top + line_w + r + scrollbar->max_value;
	//	draw_solidcircle(cx, cy1 + scrollbar->value, r, scrollbar->slider_color);


		int value1 = scrollbar->is_reverse ? scrollbar->max_value : 0;
		scrollbar->value = scrollbar->is_reverse ? scrollbar->max_value : 0;
		Bresenham_circle_iterator_init(x, y, r);
		for (int i = 0; i < r; i++) {
			while (i != x) { Bresenham_circle_iterator(x, y); } int s = y;
			draw_line_vertical(cx + i, cy1 + value1 - s, cy1 + value1 + s, 1, scrollbar->slider_color); // 绘制滚动后的滑块
			draw_line_vertical(cx - i, cy1 + value1 - s, cy1 + value1 + s, 1, scrollbar->slider_color);
		}

	}
	else {   // 横状态
		int line_w = scrollbar->leave.line_width;

		int left = scrollbar->area.left;
		int top = scrollbar->area.top + scrollbar->size.height / 2 - scrollbar->thickness / 2 - line_w;
		int h = scrollbar->thickness + line_w * 2;
//		int w = scrollbar->size.width;
		int r = h / 2;  //边框圆角半径

		int r2 = h / 2;                               // 大半径
		int r1 = r2 - scrollbar->leave.line_width;  // 小半径
		int c_l = left + r;                // 圆心的上下左右位置
		int c_r = left + r + scrollbar->max_value;
		int c_t = top + h / 2;
		fill_bar_horizontal(r1, r2, c_t, c_l, c_r, scrollbar->leave.line_color, scrollbar->background_color);

		// 绘制中间的滑块。
		r = scrollbar->thickness / 2;  // 滑块半径
		int rr = r * r;
		int cy = top + h / 2;
		int cx1 = left + line_w + r;
		int cx2 = left + line_w + r + scrollbar->max_value;
	//	draw_solidcircle(cx1 + scrollbar->value, cy, r, scrollbar->slider_color);

		int value1 = scrollbar->is_reverse ? scrollbar->max_value : 0;
		scrollbar->value = scrollbar->is_reverse ? scrollbar->max_value : 0;
		Bresenham_circle_iterator_init(x, y, r);
		for (int i = 0; i < r; i++) {
			while (i != x) { Bresenham_circle_iterator(x, y); } int s = y;
			draw_line_horizontal(cy + i, cx1 + value1 - s, cx1 + value1 + s, 1, scrollbar->slider_color); // 绘制滚动后的滑块
			draw_line_horizontal(cy - i, cx1 + value1 - s, cx1 + value1 + s, 1, scrollbar->slider_color);
		}

	}
}


/// <summary>
/// 滚动条控件初始化。样式使用默认样式。初始化后将控件插入链表。
/// </summary>
/// <param name="progressbar">滚动条控件</param>
/// <param name="x">滚动条位置x轴坐标</param>
/// <param name="y">滚动条位置y轴坐标</param>
/// <param name="w">滚动条位置宽</param>
/// <param name="h">滚动条位置高</param>
void progressbar_init(ProgressBar* progressbar, int x, int y, int w, int h)
{
	// 
	if (w < 4)w = 4;  // 这里限制滑块的最小尺寸。
	if (h < 4)h = 4;  // 再小就没有使用意义了。

	progressbar->status = (w > h) ? 0 : 1;		//如果不设置，则根据滑块尺寸自动判断状态。0表示横，1表示竖。设置status会自动调整size，使得滑块至少有空间。
	progressbar->style = default_leave_style;

	progressbar->location.left = x;
	progressbar->location.top = y;
	progressbar->size.width = w;
	progressbar->size.height = h;

	progressbar->background_color = RGB_lightgray; //  RGB_white;
	progressbar->slider_color = RGB_green;

	progressbar->is_reverse = false;

	//
	int line_width = 2;
	if (Min(w, h) < 6)line_width = 1;

	progressbar->style.line_width = line_width;  //滑块固定线宽。
	progressbar->thickness = (w > h) ? (h - line_width * 2) : (w - line_width * 2);	// 滑块的厚度。是固定值Thickness_Of_Bar。  |_____■_____________|
	progressbar->min_value = 0;						//滑块的值，最小是0.
	progressbar->max_value = (progressbar->status == 0) ? (w - progressbar->thickness - line_width * 2) : (h - progressbar->thickness - line_width * 2);  //最大的值，根据滑块大小确定。

	progressbar->percent = 0;

	area_init(&progressbar->area, &progressbar->location, &progressbar->size);
}

/// <summary>
/// 滚动条第一次绘制
/// </summary>
/// <param name="progressbar">滚动条</param>
void progressbar_firstdraw(ProgressBar* progressbar)
{
	// 画在中间。

	if (progressbar->status) {  // 竖直状态。
		int line_w = progressbar->style.line_width;

		int left = progressbar->area.left + (progressbar->size.width - progressbar->thickness) / 2 - line_w;
		int top = progressbar->area.top;
		int w = progressbar->thickness + line_w * 2;
//		int h = progressbar->size.height;
		int r = w / 2;

		int r2 = w / 2;                               // 大半径
		int r1 = r2 - progressbar->style.line_width;  // 小半径
		int c_l = left + w / 2;                // 圆心的上下左右位置
		int c_t = top + r;
		int c_b = top + r + progressbar->max_value;
		fill_bar_vertical(r1, r2, c_l, c_t, c_b, progressbar->style.line_color, progressbar->background_color);

		// 绘制中间的滑块。
		r = progressbar->thickness / 2;  // 滑块半径
		int rr = r * r;
		int cx = left + w / 2;
		int cy1 = top + line_w + r;
		int cy2 = top + line_w + r + progressbar->max_value;
		//	draw_solidcircle(cx, cy1 + progressbar->value, r, progressbar->slider_color);

		int value1 = progressbar->is_reverse ? progressbar->max_value : progressbar->min_value;
		Bresenham_circle_iterator_init(x, y, r);
		for (int i = 0; i < r; i++) {
			while (i != x) { Bresenham_circle_iterator(x, y); } int s = y;
			draw_line_vertical((cx + i), (cy1 + value1 - s), (cy1 + value1 + s), 1, progressbar->slider_color); // 绘制滚动后的滑块
			draw_line_vertical((cx - i), (cy1 + value1 - s), (cy1 + value1 + s), 1, progressbar->slider_color);
		}
		graphics_draw_point((r + cx), (cy1 + value1), progressbar->slider_color);
		graphics_draw_point((-r + cx), (cy1 + value1), progressbar->slider_color);

	}
	else {   // 横状态
		int line_w = progressbar->style.line_width;

		int left = progressbar->area.left;
		int top = progressbar->area.top + (progressbar->size.height  - progressbar->thickness) / 2 - line_w;
		int h = progressbar->thickness + line_w * 2;
//		int w = progressbar->size.width;
		int r = h / 2;  //边框圆角半径

		int r2 = h / 2;                               // 大半径
		int r1 = r2 - progressbar->style.line_width;  // 小半径
		int c_l = left + r;                // 圆心的上下左右位置
		int c_r = left + r + progressbar->max_value;
		int c_t = top + h / 2;
		fill_bar_horizontal(r1, r2, c_t, c_l, c_r, progressbar->style.line_color, progressbar->background_color);

		// 绘制中间的滑块。
		r = progressbar->thickness / 2;  // 滑块半径
		int rr = r * r;
		int cy = top + h / 2;
		int cx1 = left + line_w + r;
		int cx2 = left + line_w + r + progressbar->max_value;
		//	draw_solidcircle(cx1 + progressbar->value, cy, r, progressbar->slider_color);

		int value1 = progressbar->is_reverse ? progressbar->max_value : progressbar->min_value;
		Bresenham_circle_iterator_init(x, y, r);
		for (int i = 0; i < r; i++) {
			while (i != x) { Bresenham_circle_iterator(x, y); } int s = y;
		//for (int i = 0; i <= r; i++) {
		//	int s = sqrtf(rr - i * i);
			draw_line_horizontal((cy + i), (cx1 + value1 - s), (cx1 + value1 + s), 1, progressbar->slider_color); // 绘制滚动后的滑块
			draw_line_horizontal((cy - i), (cx1 + value1 - s), (cx1 + value1 + s), 1, progressbar->slider_color);
		}
		graphics_draw_point((cx1 + value1),(r + cy),  progressbar->slider_color);
		graphics_draw_point((cx1 + value1),(-r + cy),  progressbar->slider_color);

	}
}

/// <summary>
/// 更新进度条百分比
/// </summary>
/// <param name="progressbar">进度条</param>
/// <param name="percent">新的百分比</param>
void progressbar_change(ProgressBar* progressbar, float percent)   // 
{
	if (percent < 0)percent = 0;
	if (percent > 1)percent = 1;
	if (percent == progressbar->percent)return;

	//float value0 = progressbar->percent * progressbar->max_value; // 原来的位置
	//float value1 =              percent * progressbar->max_value; // 新的位置

	int value0;
	int value1;
	if (progressbar->is_reverse) {
		value0 = (1 - progressbar->percent) * progressbar->max_value; // 原来的位置
		value1 = (1 - percent) * progressbar->max_value; // 新的位置
	}
	else
	{
		value0 = progressbar->percent * progressbar->max_value; // 原来的位置
		value1 =              percent * progressbar->max_value; // 新的位置
	}


	progressbar->percent = percent;
	
	if (progressbar->status) { //竖滑块
		int line_w = progressbar->style.line_width;

		int left = progressbar->area.left + (progressbar->size.width  - progressbar->thickness )/ 2 - line_w;
		int top = progressbar->area.top;
		int w = progressbar->thickness + line_w * 2;
//		int h = progressbar->size.height;
		int r = progressbar->thickness / 2;  // 滑块半径
		int rr = r * r;
		int cx = left + w / 2;
		int cy1 = top + line_w + r;
		int cy2 = top + line_w + r + progressbar->max_value;

		if (progressbar->is_reverse) {  //  反向。
			Bresenham_circle_iterator_init(x, y, r);
			for (int i = 0; i < r; i++) {
				while (i != x) { Bresenham_circle_iterator(x, y); } int s = y;
			//for (int i = 0; i <= r; i++) {
			//	int s = sqrtf(rr - i * i);
				if (value0 > value1) {
					draw_line_vertical((cx + i), (cy1 + value1 - s), (cy1 + value0 - s), 1, progressbar->slider_color); //
					draw_line_vertical((cx - i), (cy1 + value1 - s), (cy1 + value0 - s), 1, progressbar->slider_color);
				}
				else {
					draw_line_vertical((cx + i), (cy1 + value1 - s), (cy1 + value0 - s), 1, progressbar->background_color); //消隐多余部分
					draw_line_vertical((cx - i), (cy1 + value1 - s), (cy1 + value0 - s), 1, progressbar->background_color);
				}
			}
		}
		else
		{
			Bresenham_circle_iterator_init(x, y, r);
			for (int i = 0; i < r; i++) {
				while (i != x) { Bresenham_circle_iterator(x, y); } int s = y;
			//for (int i = 0; i <= r; i++) {
			//	int s = sqrtf(rr - i * i);
				if (value0 > value1) {
					draw_line_vertical((cx + i), (cy1 + value1 + s), (cy1 + value0 + s), 1, progressbar->background_color); //消隐多余部分
					draw_line_vertical((cx - i), (cy1 + value1 + s), (cy1 + value0 + s), 1, progressbar->background_color);
				}
				else {
					draw_line_vertical((cx + i), (cy1 + value1 + s), (cy1 + value0 + s), 1, progressbar->slider_color); //
					draw_line_vertical((cx - i), (cy1 + value1 + s), (cy1 + value0 + s), 1, progressbar->slider_color);
				}
			}
		}


	}
	else { //横滑块
		int line_w = progressbar->style.line_width;

		int left = progressbar->area.left;
		int top = progressbar->area.top +( progressbar->size.height - progressbar->thickness )/ 2 - line_w;
		int h = progressbar->thickness + line_w * 2;
//		int w = progressbar->size.width;
		int r = progressbar->thickness / 2;  // 滑块半径 
		int rr = r * r;
		int cy = top + h / 2;
		int cx1 = left + line_w + r;
		int cx2 = left + line_w + r + progressbar->max_value;
		
		if (progressbar->is_reverse) {  //  反向。
			Bresenham_circle_iterator_init(x, y, r);
			for (int i = 0; i < r; i++) {
				while (i != x) { Bresenham_circle_iterator(x, y); } int s = y;
			//for (int i = 0; i <= r; i++) {
			//	int s = sqrtf(rr - i * i);
				if (value0 > value1) {
					draw_line_horizontal(cy + i, (cx1 + value1 - s), (cx1 + value0 - s), 1, progressbar->slider_color); //
					draw_line_horizontal(cy - i, (cx1 + value1 - s), (cx1 + value0 - s), 1, progressbar->slider_color); //
				}
				else {
					draw_line_horizontal(cy + i, (cx1 + value1 - s), (cx1 + value0 - s), 1, progressbar->background_color); //消隐多余部分
					draw_line_horizontal(cy - i, (cx1 + value1 - s), (cx1 + value0 - s), 1, progressbar->background_color); //消隐多余部分
				}
			}
		}
		else {
			Bresenham_circle_iterator_init(x, y, r);
			for (int i = 0; i < r; i++) {
				while (i != x) { Bresenham_circle_iterator(x, y); } int s = y;
			//for (int i = 0; i <= r; i++) {
			//	int s = sqrtf(rr - i * i);
				if (value0 > value1) {
					draw_line_horizontal(cy + i, (cx1 + value1 + s), (cx1 + value0 + s), 1, progressbar->background_color); //消隐多余部分
					draw_line_horizontal(cy - i, (cx1 + value1 + s), (cx1 + value0 + s), 1, progressbar->background_color); //消隐多余部分
				}
				else {
					draw_line_horizontal(cy + i, (cx1 + value1 + s), (cx1 + value0 + s), 1, progressbar->slider_color); //
					draw_line_horizontal(cy - i, (cx1 + value1 + s), (cx1 + value0 + s), 1, progressbar->slider_color); //
				}
			}
		}

	}
}


/// <summary>
/// 初始化绘图设备，初始化GUI。
/// </summary>
void gui_init()
{
	//default_hovor_style.back_color = RGB_crtl_blue;
	//default_hovor_style.line_color = RGB_ctrl_purple;
	//default_hovor_style.line_width = 2;
	//default_leave_style.back_color = RGB_crtl_blue;
	//default_leave_style.line_color = RGB_line_blue;
	//default_leave_style.line_width = 2;
	//default_window_style.back_color = RGB_window_blue;
	//default_window_style.line_color = RGB_line_blue;
	//default_window_style.line_width = 2;


	graphics_init(); //初始化绘图设备
	GUI_dlist_init(&list_Button);//, NULL);//初始化按键链表
	GUI_dlist_init(&list_ScrollBar);//, NULL);//初始化滑块链表
}


/// <summary>
/// 将鼠标消息mouseMsg分发给各控件
/// </summary>
void dispatchMsg(int x,int y)
{
	GUI_dlistNode* listnode;
	Button* button;
	ScrollBar* scrollbar;
	//判断是否执行了任务
	bool bAtwork = false;

	//先遍历按键
	if (bAtwork == false)  //未执行任务，可以重新扫描控件，分配信息。
	{
		if (list_Button.count > 0) {
			listnode = list_Button.head;
			while (listnode != NULL) {
				button = GUI_container_of(listnode, Button, node);
				if (is_enter_area(&button->area, x, y)) //鼠标在控件上
				{
					if (button->is_hovor != true)	//若鼠标在控键上的标志为0，说明鼠标刚进入按键
					{
						button->is_hovor = true;    //鼠标在控键上的标志位置1
						button->is_selected = true; 
						button_draw_hovor(button);
						button_onMouseMsg(button);  // 按键只响应一次。没有长按的响应效果。
					}
					bAtwork = true;
					break;			//一次只响应一个控键
				}
				else   ////鼠标不在控件上
				{
					if (button->is_hovor == true)	//若鼠标在控键上的标志为1，说明鼠标刚离开按键
					{
						button->is_hovor = false;    //鼠标在控键上的标志位置1
						button->is_selected = false;
						button_draw_leave(button);
					}
				}
				listnode = listnode->next;
			}
		}
	}
	//再遍历滑块
	if (bAtwork == false)  //未执行任务，可以重新扫描控件，分配信息。
	{
		if (list_ScrollBar.count > 0) {
			listnode = list_ScrollBar.head;
			while (listnode != NULL) {
				scrollbar = GUI_container_of(listnode, ScrollBar, node);
				if (is_enter_area(&scrollbar->area, x,y)) //鼠标在控件上
				{
					if (scrollbar->is_hovor == false)	//若鼠标在控键上的标志为0，说明鼠标刚进入按键
					{
						scrollbar->is_hovor = true;    //鼠标在控键上的标志位置1
						scrollbar_draw_hovor(scrollbar);

						if (scrollbar->status == 0) //横，记录x坐标 。滑块刚响应的时候，需要先刷新一下newMousePoint的值。
						{
							scrollbar->newMousePoint = x;
						}
						else
						{
							scrollbar->newMousePoint = y;
						}

					}
					scrollbar_onMouseMsg(scrollbar, x, y);  // 滑块滑动，选中之后可以多次响应。
					
					bAtwork = true;
					break;			//一次只响应一个控键
				}
				else   ////鼠标不在控件上
				{
					if (scrollbar->is_hovor == true)	//若鼠标在控键上的标志为1，说明鼠标刚离开按键
					{
						scrollbar->is_hovor = false;    //鼠标在控键上的标志位置1
						scrollbar_draw_leave(scrollbar);
					}
				}
				listnode = listnode->next;
			}
		}
	}
}
//

/*
初始化控件，绘制好后，实时跟新鼠标信息，调用dispatchMsg即可。
下面就是怎么跟新鼠标信息了。要同步所有信息。
下面是测试函数
*/

////////////           //////////////





Button button0;
Button button1;
Button button2;
Button button3;
Button button4;
Button button5;
Lable lableb0;  // x.x

ScrollBar scrollbar0;
ProgressBar progressbar0;
void print_slider0()
{
	progressbar_change(&progressbar0, (float)scrollbar0.value / scrollbar0.max_value);
}

//选择按键
void bbtn_option()
{
	printfs(" btn click ");
}
//确认按键
void bbtn_check() {

}

//取消按键
void bbtn_cancle() {
}

//启动按键
void bbtn_start() {
}

//up
void bbtn_up() {
}

//dowm
void bbtn_down() {
}




void init_ui()
{
	// 初始化按键
	{
		button_init(&button0, 10, 260, 30, 30);
		button_init(&button1, 50, 260, 30, 30);
		button_init(&button2, 90, 260, 30, 30);
		button_init(&button3, 130, 260, 30, 30);
		button_init(&button4, 170, 260, 30, 30);
		button_init(&button5, 210, 260, 30, 30);
		button0.onClick = bbtn_option;
		button1.onClick = bbtn_check;
		button2.onClick = bbtn_cancle;
		button3.onClick = bbtn_up;
		button4.onClick = bbtn_down;
		button5.onClick = bbtn_start;


		button_firstdraw(&button0);
		button_firstdraw(&button1);
		button_firstdraw(&button2);
		button_firstdraw(&button3);
		button_firstdraw(&button4);
		button_firstdraw(&button5);
		// button0.onClick = ;

		TextFont textfont = {
		16,
		0,  // 默认透明
		RGB_white,
		RGB_black
		};

		lable_init(&lableb0, 10, 300, textfont.size / 2 * 5 * 6 + 2, textfont.size * 2 + 2);
		lable_init_text(&lableb0, "opt check cancle up down start", 5 * 6, textfont);

		lable_draw(&lableb0);
	}

	//初始化滚动条
	scrollbar_init(&scrollbar0, 20, 350, 200, 41);
	scrollbar0.onClick = print_slider0;
	//	scrollbar0.is_reverse = true;
	scrollbar_firstdraw(&scrollbar0);
	// scrollbar0.onClick = print_slider0;

	// 初始化进度条
	progressbar_init(&progressbar0, 20, 400, 200, 41);
	// progressbar0.is_reverse = true;
	progressbar_firstdraw(&progressbar0);
}
//


#define fundata_len 300  
float x_1[fundata_len] = { 0 };
float x_2[fundata_len] = { 0 };
float y_1[fundata_len] = { 0 };
float y_2[fundata_len] = { 0 };


#include"gl_.h"
#include"mcu_.h"
#include"math.h"
void gui_test()
{
	// 建立一个角度，方向的绘图。
//	gui_init();


	for (int i = 0; i < fundata_len; i++) {
		x_1[i] = i;
		x_2[i] = i;
		y_1[i] = 0; // 3 * c_sin(x1[i] / 10);
		y_2[i] = 0; // 4 * c_sin(x2[i] / 30) + 1 * c_cos(x2[i] / 40) + 5;

		//graphics_fill_area(0, 0, 300, 300, RGB_lightgray);
		//draw_bar_horizontal(11,i,130,30,160,RGB_blue);
		//GD_wait_key_input();

	}

	TwoFunctionPicture tp;
	TwoFunctionPicture tp2;

	{
		char title[] = "Curve Chart";          //标题
		char xtitle[] = "time(s)";          //x轴标题
		char y1title[] = "step";           //y轴标题
		char y2title[] = "speed";          //y轴标题
		TwoFunctionPicture_init(&tp, 250, 0, 500, 650);
		TwoFunctionPicture_draw_title(&tp, title, xtitle, y1title, y2title);
		TwoFunctionPicture_fast_draw_func(&tp, x_1, y_1, fundata_len, x_2, y_2, fundata_len, 0, 0,
			1,
			-10, fundata_len,
			-50, fundata_len,
			-50, 650
		);
	}


	int x, y;
	init_ui();
	int t = 0;
	unt32 timestamp = mcu_time_ms();
	while (1) // (i < 20000)
	{
		t++;
	//	mcu_delay_ms(1);
		if (t % 10 == 0) {
			//消息派遣
			
			if (GD_is_touched(&x, &y)) {
				dispatchMsg(x, y);
				timestamp = mcu_time_ms();
			}
			else if(mcu_time_ms() - timestamp > 100) { // 超过一段时间没有按下
				timestamp = unt32_MAX_number;
				dispatchMsg(-1, -1);   // 这样才能使控件在没有按下的时候恢复显示。
				//	printf("b");
			}
		}

		if (t % 20 == 0 && 0) 
		{
			static int si = 0;
			si++;
			float nx = si % fundata_len;
			float ny1 = sinf(0.09f * si) * 40.f;
			float ny2 = cosf(0.17f * si) * 20.f;
			TwoFunction_replace_point(&tp.draw, si % fundata_len, nx, ny1, true);
			TwoFunction_replace_point(&tp.draw, si % fundata_len, nx, ny2, false);
		//	printfs(" \r\n  step = %f,  frequency = %f ", ny1, ny2);

		}

	}


}
//





